package com.component;

import com.Listener.ActionDoneListener;
import com.bean.Farmer;
import com.bean.ProductCategory;
import com.bean.Users;
import com.dao.*;
import com.util.ScreenUtils;
import com.util.SelectFarmer;
import com.util.SelectProductCategory;
import com.util.SelectUsers;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Locale;

public class UpdateProductCategoryDialog extends JDialog {
    final int WIDTH = 500;
    final int HEIGHT = 400;

    private String categoryID;

    private JTextField IDField;
    private JTextField nameField;
    private JTextField remaksField;

    private ActionDoneListener listener;

    public UpdateProductCategoryDialog(JFrame jf, String title, boolean isModel, ActionDoneListener listener,String categoryID){
        super(jf,title,isModel);
        this.listener  = listener;
        this.categoryID = categoryID;

        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装农产品类别编号
        Box IDBox = Box.createHorizontalBox();
        JLabel IDLabel = new JLabel("类别编号：");
        IDField = new JTextField(15);

        IDBox.add(IDLabel);
        IDBox.add(Box.createHorizontalStrut(15));
        IDBox.add(IDField);

        //组装类别名称
        Box nameBox = Box.createHorizontalBox();
        JLabel nameLabel = new JLabel("类别名称：");
        nameField = new JTextField(15);

        nameBox.add(nameLabel);
        nameBox.add(Box.createHorizontalStrut(15));
        nameBox.add(nameField);

        //组装备注
        Box remaksBox = Box.createHorizontalBox();
        JLabel remaksLabel = new JLabel(" 备       注 ：");
        remaksField = new JTextField(15);

        remaksBox.add(remaksLabel);
        remaksBox.add(Box.createHorizontalStrut(15));
        remaksBox.add(remaksField);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton addBtn = new JButton("修改");
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户的录入
                int ID = Integer.parseInt(IDField.getText().trim());
                String categoryName = nameField.getText().trim();
                String remarks = remaksField.getText().trim();

                //修改数据库中的数据
                if(IDField.getText().trim().length()!=4){
                    JOptionPane.showMessageDialog(jf, "类别编号要为四位数字！");
                }
                else if(!new SelectProductCategory().notexitProductCategoryID(ID)){
                    JOptionPane.showMessageDialog(jf,"该类别编号已存在！");
                }
                else{
                    ProductCategory productCategory = new ProductCategory(ID,categoryName,remarks);
                    new UpdateProductCategory().updateProductCategory(productCategory,Integer.parseInt(categoryID));
                    JOptionPane.showMessageDialog(jf, "修改成功!");
                    listener.done(null);
                    dispose(); // 关闭添加窗口
                }
            }
        });


        btnBox.add(addBtn);

        vBox.add(Box.createVerticalStrut(40));
        vBox.add(IDBox);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(nameBox);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(remaksBox);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(btnBox);

        //为了左右有间距，在vBox外层封装一个水平的Box，添加间隔
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(50));
        hBox.add(vBox);
        hBox.add(Box.createHorizontalStrut(50));

        requestData();

        this.add(hBox);
    }
    //请求数据
    public void requestData(){
        List<ProductCategory> list = new SelectProductCategory().getProductCategoryData();
        for(ProductCategory productCategory:list)
        {
            if(Integer.toString(productCategory.getCategoryID()).equals(categoryID)){
                IDField.setText(categoryID);
                nameField.setText(productCategory.getCategoryName());
                remaksField.setText(productCategory.getRemarks());
                break;
            }
        }
    }

}
